﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Overflow_RTS.Utilities;

namespace Overflow_RTS.Entities
{

    class Entity
    {
        Vector2 pos = Vector2.Zero;
        Vector2 dir = Vector2.Zero;
        Vector2 destination = Vector2.Zero;
        Vector2 depart = Vector2.Zero;

        Rectangle hitbox = new Rectangle(-10,-10,-1,-1);
        float vel = 0.5f;

        String image = "Images/Tests/Zozor";

        SpriteBatch sBatch;
        Primitives2d p2D;

        public bool selected;

        public Entity(SpriteBatch spriteBatch, Primitives2d primitive2d)
        {
            sBatch = spriteBatch;
            p2D = primitive2d;
        }

        //*****************************************
        // GETs AND SETS
        //*****************************************
        public Rectangle getHitbox()
        { return hitbox; }

        public String getImg()
        {
            return image;
        }

        public Vector2 getPos()
        {
            return pos;
        }

        public Vector2 getDepart()
        {
            return depart;
        }

        public float getVel()
        {
            return vel;
        }

        public void setVel(float v)
        {
            vel = v;
        }

        public Vector2 getDestination()
        {
            return destination;
        }

        public void setDirection(Vector2 nDir)
        {
            dir = Vector2.Normalize(nDir);
        }

        public void setDepart()
        {
            depart = pos;
        }

        // MAIN UPDATE MANAGEMENT
        public void update(GameTime gameTime)
        {
            move(gameTime);

        }

        // OTHER
        private void manageDestination()
        {
            float distance = (getDestination() - getPos()).Length();

            if (distance < 200 && vel >= 0.01f)
            { vel -= 0.01f; }
            if (distance < 5)
            { vel = 0; }
            
        }

        public void setDestination(float X, float Y)
        {
            destination.X = X;
            destination.Y = Y;
        }

        public void move(GameTime gameTime)
        {
            pos += dir * vel * (float)gameTime.ElapsedGameTime.TotalMilliseconds;
            manageDestination();
            hitbox = new Rectangle((int)pos.X, (int)pos.Y, 75, 75);
        }

        public void draw(Texture2D img)
        {
            sBatch.Draw(img, pos, Color.White);
            if (selected)
            {
                p2D.DrawRectangle(hitbox, Color.LightGreen, false);
            }
        }
    }
}
